package com.kz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.kz.model.Camera;
import com.kz.service.DBOpenHelper;
import com.kz.service.Lightservice;

public class CameraListActivity extends Activity {
   private Lightservice lightservice;
   
	
   
   
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        DBOpenHelper dbOpenHelper = new DBOpenHelper(this);
		
    	dbOpenHelper.getWritableDatabase();
        
         setContentView(R.layout.cameralist);
         this.lightservice = new Lightservice(this);
         List<Camera> cameras = lightservice.getScrollData(0, 5);
        ListView listview = (ListView)this.findViewById(R.id.listView);
        
        List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
        
        for(Camera camera : cameras){
     	   HashMap<String,Object> item = new HashMap<String,Object>();
     	   item.put("id", camera.getId());
     	   item.put("addr1", camera.getRoadname1());
     	   item.put("addr2", camera.getRoadname2());
     	   item.put("direction", camera.getDirection());
     	   data.add(item);
        }
        
        
        
        SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.listitem,
     		   new String[]{"id","addr1","addr2","direction"},new int[]{R.id.id,R.id.addr1,R.id.addr2,R.id.direction}
        );
        
        listview.setAdapter(adapter);
        
        
        listview.setOnItemClickListener(new OnItemClickListener() {  
        	  
            @Override  
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                    long arg3) {  
                setTitle("点击第"+arg2+"个项目");  
            }  
        });
        
        
        listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){  
            
          @Override  
          public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
              menu.setHeaderTitle("请修改这个Camera的属性");    
              menu.add(0, 0, 0, "修改 Camera的属性");  
              menu.add(0, 1, 0, "增加 Camera的");  
              menu.add(0, 2, 0, "删除选定 Camera");  
          }  
        });   
    }  
      
    //长按菜单响应函数  
    @Override  
    public boolean onContextItemSelected(MenuItem item) {  
       // setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");
    	Toast.makeText(this, "modify", 2).show();
    	
    	return super.onContextItemSelected(item);  
    }  
    
    
    
    
    
}  
